/**
 * @Title: LoginAction.java 
 * @Package com.qtong.cssp.actions 
 * @Description: TODO 
 * @author wangxi_qt
 * @date 2014-3-25 上午10:12:41 
 * @version V1.0 
 */
package com.bh.action;

import java.io.IOException;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.bh.biz.LoginBiz;


/**
 * @Title: LoginAction.java 
 * @Package com.qtong.cssp.actions 
 * @Description: TODO 
 * @author wangxi_qt
 * @date 2014-3-25 上午10:12:41 
 * @version V1.0 
 */
public class LoginAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static String LOGIN_ERR="55So5oi35ZCN5oiW5a+G56CB6ZSZ6K+v";//base64转码"用户名或密码错误"
	
	private String loginname;
	private String loginpwd;
	
	private String resultmsg;
	
	/**
	 * 用户登录
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String login() throws ServletException, IOException {
		LoginBiz biz=new LoginBiz();
		Map<String, Object> map=biz.login(loginname, loginpwd);
		switch ((Integer)map.get("result")) {
		case 1:
			HttpServletRequest req=ServletActionContext.getRequest();
			HttpSession session=req.getSession(true);
			session.setAttribute("loginname", map.get("loginname"));
			session.setAttribute("loginpwd", map.get("loginpwd"));
			session.setAttribute("username", map.get("username"));
			session.setAttribute("usertel", map.get("usertel"));
			session.setAttribute("userorg", map.get("userorg"));
			session.setAttribute("lastlogintime", map.get("lastlogintime"));
			return "login-success";

		default:
			setResultmsg(LOGIN_ERR);
			return "login-error";
		}
		
		
		
	}

	public void chk() {
		HttpServletRequest req=ServletActionContext.getRequest();
		HttpSession session=req.getSession();
		JSONObject jo=new JSONObject();
		if (((String)session.getAttribute("loginname"))!=null) {
			jo.put("result", "true");
			jo.put("loginname", (String)session.getAttribute("loginname"));
			jo.put("username", (String)session.getAttribute("username"));
			jo.put("userorg", (String)session.getAttribute("userorg"));
			jo.put("lastlogintime", (String)session.getAttribute("lastlogintime"));
		}else {
			jo.put("result", "false");
		}
		this.out(jo.toString());
		
	}
	public String logout() {
		HttpServletRequest req=ServletActionContext.getRequest();
		HttpSession session=req.getSession();
		session.removeAttribute("loginname");
		return "logout-success";
	}
	
	
	
	public void setResultmsg(String resultmsg) {
		this.resultmsg = resultmsg;
	}

	public String getLoginpwd() {
		return loginpwd;
	}


	public void setLoginpwd(String loginpwd) {
		this.loginpwd = loginpwd;
	}

	public String getResultmsg() {
		return resultmsg;
	}

	public String getLoginname() {
		return loginname;
	}

	public void setLoginname(String loginname) {
		this.loginname = loginname;
	}

}
